package com.xinnet.mms.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.CustomerCare;
import com.xinnet.mms.entity.EmpCustomRelation;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.entity.PublicConfig;

public class AppGlobalContainer {
	/**
	 * 产品规则Map  Key 产品ID, value Product
	 */
	public static Map<String,Product> productRuleMap = null;
	
	/**
	 * 流转客户Map Key客户类型 VALUE 客户列表 List<EmpCustomRelation>
	 */
	public static Map<Integer,List<EmpCustomRelation>> transferCustomMap = null;

	/**
	 *EmpCustomRelation key 客户ID ,value：客户列表 List<EmpCustomRelation>
	 */
	public static Map<String,List<EmpCustomRelation>> empCustomRelationMap = null;
	
	/**
	 * 业绩保留期:key:scoreHoldTime value:PublicConfig
	 */
	public static Map<String,PublicConfig> scoreHoldDateMap = null;
	
	public static Map<String,Product> canPushProductMap = null;
	
	/**
	 * 状态历史：key 客户ID ,value 状态历史列表List<CustomStateHist>
	 */
	public static Map<String,List<CustomStateHist>> customStateHistMap = null;
	
	/**
	 * 订单MAP key:custom_id+productCode value:
	 */
	public static Map<String,CustomerCare> customerCareMap = new HashMap<String, CustomerCare>();
	
	/**
	 * 被删除的订单MAP key:custom_id+productCode value:
	 */
	public static Map<String,CustomerCare> delCustomerCareMap = null;
	
	
	/**
	 * 部门组织结构json MAP key:部门id value: json
	 */
	public static Map<String,String> deptIdToDeptJsonMap= new HashMap<String, String>();
	
	public static Map<String,String> is400AllotMap = new HashMap<String, String>();
	
	public static Product getPushProduct(Product currentProductRule,List<CustomStateHist> customStateHistList){
		List<Product> enablePushProductList =new ArrayList<Product>();
		String  currentProductId = currentProductRule.getId();
		
		for (Iterator<String> iterator = productRuleMap.keySet().iterator(); iterator.hasNext();) {
			String tempProductId = (String) iterator.next();
			//String tempProductId = product.getId();
			boolean isEnable=true;
			if(customStateHistList!=null && customStateHistList.size()>0){
				for (Iterator<CustomStateHist> iterator2 = customStateHistList.iterator(); iterator2.hasNext();) {
					CustomStateHist customStateHist = (CustomStateHist)iterator2.next();
					String pushProductId = customStateHist.getProductId();
					if(pushProductId==null){
						isEnable=true;
						break;
					}
					if(pushProductId.equals(tempProductId) || currentProductId.equals(tempProductId)){
						isEnable=false;
						break;	
					}
				}
				if(isEnable){
					Product product = productRuleMap.get(tempProductId);
					enablePushProductList.add(product);
				}
			}else{
				Product tempProduct = productRuleMap.get(tempProductId);
				String id= tempProduct.getId();
				if(!tempProductId.equals(id)){
					enablePushProductList.add(tempProduct);
				}
			}
		}
		if(enablePushProductList.size()>0){
			//推送产品
			Collections.sort(enablePushProductList, new Comparator <Product>(){  
	            @Override  
	            public int compare(Product p1, Product p2) {  
	                return p1.getPriority().compareTo(p2.getPriority());  
	            }  
			 });
			Product product = enablePushProductList.get(0);
			return product;
		}
		return null;
	}
}
